//
//  行前准备总结
//

#import "PreparedController.h"
#import "PrepareModel.h"
#import "CompanyRecruitModel.h"
#import "fengongTableViewCell.h"
@interface PreparedController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,assign)int currentPage; //当前页
@property(nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)UITableView *tab;
@end

@implementation PreparedController

static NSString *const ID = @"CompanyRecruitCell";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
NSLog(@"-------%f",self.view.height);
    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 5, KScreenWidth-10, (KScreenHeight-80-64-46-10)/2)];
    
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    
     UIView *bgv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth - 20, 30)];
    bgv.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgv];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0,KScreenWidth - 20,30)];
    label.text = @"企业回访及成果追踪";
    
    label.textColor = [UIColor blueColor];
    
    label.font = [UIFont systemFontOfSize:15];
    
    [bgv addSubview:label];
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 4, 10)];
    vi.backgroundColor = [UIColor blueColor];
    
    [bgv addSubview:vi];

    [view addSubview:bgv];
    
_tab = [[UITableView alloc]initWithFrame:CGRectMake(0, bgv.bottom, KScreenWidth-10, view.height-30) style:UITableViewStylePlain];
    _tab.delegate = self;
    _tab.dataSource = self;
    _tab.backgroundColor = [UIColor whiteColor];
    [view addSubview:_tab];
    
    
     __weak typeof(self) weakSelf = self;

     [self.tab addHeaderWithCallback:^{
        weakSelf.currentPage = 1;

        [weakSelf loadData]; //加载网络数据
    }];
    [self.tab headerBeginRefreshing];
    
    [self.tab addFooterWithCallback:^{
        
             [weakSelf loadMore];
        
    }];
anpaiview *anpai = [[anpaiview alloc]initWithFrame:CGRectMake(5, view.bottom+5, KScreenWidth-10, KScreenHeight-80-64-46-view.height-30)];
anpai.backgroundColor = [UIColor whiteColor];
anpai.id = _ID;
[self.view addSubview:anpai];
    
    
    
}
- (void)loadData{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,(long)self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
if (self.currentPage ==1) {
            [self.dataArray removeAllObjects];
        }
       
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
//    NSLog(@"%@",dict);
    [_dataArray addObject:dict];
    
}
//            _totalCounts =  _dataArray.count;
        [self.tab reloadData];
        [self.tab headerEndRefreshing];
        self.currentPage++;
        if (self.dataArray.count ==0) {
        [self.view makeToast:@"暂时还没有数据哦"];
        }
    } failure:^(NSError *error) {
        
        
    }];
    
}
- (void)loadMore{

    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,(long)self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

         self.currentPage++;
    
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
    
}

        [self.tab footerEndRefreshing];
                [self.tab reloadData];

    } failure:^(NSError *error) {
        
        
    }];
    


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

{
 static NSString *CellIdentifier = @"Cell";  

    fengongTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell==nil) {  
        cell=[[fengongTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *dic = _dataArray [indexPath.row];
    cell.dict = dic;
    return cell;
}

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;
}


@end












